﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Data.SqlClient;
using System.Data;
namespace Model
{
   public  class TableSelectorDAO
    {
        /// <summary>
        /// 获取一个表所有的字段信息
        /// </summary>
        /// <param name="conStr">连接字符串</param>
        /// <param name="sqlStr">SQL命令</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
      public  List<eBuildFieldInfo> GetFieldsByTableFromDb(string conStr, string sqlStr, string tableName)
       {
           SqlConnection con = null;
           List<eBuildFieldInfo> list = null;
           try
           {
               con = new SqlConnection(conStr);
               using (SqlCommand cmd = new SqlCommand(sqlStr, con))
               {
                   con.Open();
                   using (IDataReader dr = cmd.ExecuteReader())
                   {
                       list = new List<eBuildFieldInfo>();
                       while (dr.Read())
                       {
                           //datareader中各个字段名跟实体类中属性名相同（自己规定的规则）
                           eBuildFieldInfo ft = new eBuildFieldInfo
                           {
                               FieldName = dr["FieldName"].ToString(),
                               DataType = dr["DataType"].ToString(),
                               Description=dr["Description"].ToString(),
                               FieldLength = Convert.ToInt32(dr["FieldLength"]),
                               IsNullable = Convert.ToBoolean(dr["IsNullable"]),
                               FromTable = tableName
                           };
                           list.Add(ft);
                       }

                   }
               }
               return list;
           }
           catch (Exception ex)
           {
               throw ex;
           }
           finally
           {
               if (con != null)
               {
                   con.Close();
               }

           }

       }
    }
}
